package edu.gmu.projects.fireresponder.relevancyengine;

import java.io.IOException;
import java.io.InputStream;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import edu.gmu.projects.fireresponder.beacon.BeaconService;
import edu.gmu.projects.fireresponder.comm.MessageCommunicator;
import edu.gmu.projects.fireresponder.fusionengine.FusionService;
import edu.gmu.projects.fireresponder.missionplanner.MissionPlanner;

public class RelevanceEngineService extends Service {

	private RelevanceEngine relevance;

	public RelevanceEngine getRelevanceEngine() {
		if (relevance == null) {
			try {
				final InputStream BuildingStream = getAssets().open(
						"json/building-data.json");
				final InputStream PathStream = getAssets().open(
						"json/path-data.json");
				final InputStream SafespotStream = getAssets().open(
						"json/safespot-data.json");

				relevance = new RelevanceEngine(new MissionPlanner(
						BuildingStream, PathStream, SafespotStream));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return relevance;
	}

	private final IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public RelevanceEngineService getService() {
			return RelevanceEngineService.this;
		}
	}

	@Override
	public IBinder onBind(Intent arg) {
		return mBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		bindService(new Intent(this, FusionService.class),
				fusionServiceConnection, BIND_AUTO_CREATE);
		bindService(new Intent(this, BeaconService.class),
				beaconServiceConnection, BIND_AUTO_CREATE);

		MessageCommunicator.addFirstResponderAlertListener(getRelevanceEngine());

		Log.i("RelevanceEngineService", "Created RelevanceEngineService service");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		unbindService(fusionServiceConnection);
		unbindService(beaconServiceConnection);

		Log.i("RelevanceEngineService", "Destroyed RelevanceEngineService service");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return START_STICKY;
	}
	
	private FusionService fusionService;

	private ServiceConnection fusionServiceConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			fusionService = ((FusionService.LocalBinder) service).getService();
			fusionService.getFusionEngine().addListener(getRelevanceEngine());
			getRelevanceEngine().setFusionEngine(fusionService.getFusionEngine());
			
			Log.i("RelevanceEngineService", "Connected to FusionService");
		}

		public void onServiceDisconnected(ComponentName name) {
			fusionService.removeListener(getRelevanceEngine());
			fusionService = null;

			Log.i("RelevanceEngineService", "Disconnected from FusionService");
		}
	};

	private BeaconService beaconService;

	private ServiceConnection beaconServiceConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			beaconService = ((BeaconService.LocalBinder) service).getService();
			beaconService.addListener(relevance);
			
			Log.i("RelevanceEngineService", "Connected to BeaconService");
		}

		public void onServiceDisconnected(ComponentName name) {
			beaconService.removeListener(relevance);
			beaconService = null;

			Log.i("RelevanceEngineService", "Disconnected from BeaconService");
		}
	};
}